package myPetStore.persistence.impl;

import myPetStore.domain.Category;
import myPetStore.domain.Product;
import myPetStore.persistence.DBUtil;
import myPetStore.persistence.ProductDao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class ProductDaoImpl implements ProductDao {
    private static final String GET_PRODUCT =
            "SELECT productid,name,descn AS description,category as categoryId FROM product WHERE productid=?";
    private static final String GET_Product_LIST_BY_CATEGORY=
            "SELECT productid,name,descn as description,category as categoryId FROM product where category = ?";
    private static final String GET_Product_LIST=
            "SELECT productid,name,descn as description,category as categoryId FROM product where lower(name) like ?";
    @Override
    public List<Product> getProductListByCategory(String var1) {
        List<Product> productList = new ArrayList<>();
        try {
            Connection connection= DBUtil.getConnection();
            PreparedStatement statement=connection.prepareStatement(GET_Product_LIST_BY_CATEGORY);
            statement.setString(1,var1);
            ResultSet resultSet=statement.executeQuery();
            while (resultSet.next()){
                Product product=new Product();
                product.setProductId(resultSet.getString(1));
                product.setName(resultSet.getString(2));
                product.setDescription(resultSet.getString(3));
                product.setCategoryId(resultSet.getString(4));
                productList.add(product);

            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closeStatement(statement);
            DBUtil.closeConnection(connection);

        }catch (Exception ex){
            ex.printStackTrace();
        }
        return productList;
    }

    @Override
    public Product getProduct(String var1) {
        Product product=null;
        try{
            Connection connection=DBUtil.getConnection();
            PreparedStatement preparedStatement=connection.prepareStatement(GET_PRODUCT);
            preparedStatement.setString(1,var1);
            ResultSet resultSet=preparedStatement.executeQuery();
            if(resultSet.next()){
                product=new Product();
                product.setProductId(resultSet.getString(1));
                product.setName(resultSet.getString(2));
                product.setDescription(resultSet.getString(3));
                product.setCategoryId(resultSet.getString(4));

            }
            resultSet.close();
            preparedStatement.close();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return product;
    }

    @Override
    public List<Product> searchProductList(String var1) {
        List<Product> productList = new ArrayList<>();
        try {
            Connection connection= DBUtil.getConnection();
            PreparedStatement preparedStatement=connection.prepareStatement(GET_Product_LIST);
            preparedStatement.setString(1,var1);
            ResultSet resultSet=preparedStatement.executeQuery();
            while (resultSet.next()){
                Product product=new Product();
                product.setProductId(resultSet.getString(1));
                product.setName(resultSet.getString(2));
                product.setDescription(resultSet.getString(3));
                product.setCategoryId(resultSet.getString(4));
                productList.add(product);

            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closeStatement(preparedStatement);
            DBUtil.closeConnection(connection);

        }catch (Exception ex){
            ex.printStackTrace();
        }
        return productList;
    }
}
